import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
/**
 * 权限状态码
 * 前端跳转到不同路径
 */
export const AUTH_CODE_LICENCE = '100000'; // 没有安装证书
export const AUTH_CODE_LOGIN = '100001';   // 没有登录
export const AUTH_CODE_BCM = '100010';     // 没有部署
export const AUTH_CODE_AUDIT = '100011';   // 只能进入
export const AUTH_CODE = 450;        // 无权限状态码

const prefix = '/WebApi/launch/static/dist/#/';
export const ERROR_AUTH_LICENCE_URL = '/license';         // 安装证书路由
export const ERROR_AUTH_LOGIN_URL = 'login';       // 登录路由
export const ERROR_AUTH_BCM_URL = 'bcm/dashboard'; // 集群部署路由
export const ERROR_AUTH_AUDIT_URL = 'route/audit'; // 审计日志路由
export const SUCCESS_AUTH_URL = 'bcm/dashboard/summary';  // ISOP首页

const LOGIN_URL = '/login';
@Injectable({
    providedIn: 'root'
})
export class AuthorityService {
    /**
     * 权限判断
     * codeStatus: 请求状态码是否为无权限的状态码
     * authCode: 权限状态码状态码
     */
    judgeAuthority(authCode: string) {
        switch (authCode) {
            // case AUTH_CODE_LICENCE:
            //     window.location.href = ERROR_AUTH_LICENCE_URL;
            //     break;
            // case AUTH_CODE_LOGIN:
            //     this.router.navigateByUrl(LOGIN_URL);
            //     break;
            case AUTH_CODE_BCM:
                window.location.href = prefix + ERROR_AUTH_BCM_URL;
                break;
            case AUTH_CODE_AUDIT:
                window.location.href = prefix + ERROR_AUTH_AUDIT_URL;
                break;
            default:
                console.log('状态码：' + authCode);
        }
    }
    /**
     *  判断权限状态码，有权限则跳转到 ‘首页’
     * */
    hasAuthority(codeStatus: number, authCode: string) {
        if (codeStatus === AUTH_CODE) {
            this.judgeAuthority(authCode);
        } else {
            console.log(`/isop/system/status/ 状态码: ${codeStatus}`);
        }
    }
    /**
     *  判断权限状态码，有权限 ‘不跳转’
     * */
    pureAuthority(codeStatus: number, authCode: string) {
        if (codeStatus === AUTH_CODE) {
            this.judgeAuthority(authCode);
        } else {
            console.log(`/isop/system/status/ 状态码: ${codeStatus}`);
        }
    }
    constructor(private router: Router) { }
}
